package com.gym.myshop.web.controller;

import com.gym.myshop.common.Const;
import com.gym.myshop.entity.Address;
import com.gym.myshop.entity.Cart;
import com.gym.myshop.entity.Orders;
import com.gym.myshop.entity.User;
import com.gym.myshop.services.AddressSerives;
import com.gym.myshop.services.CartServices;
import com.gym.myshop.services.OrderServices;
import com.gym.myshop.services.impl.AddressSerivesImpl;
import com.gym.myshop.services.impl.CartServicesImpl;
import com.gym.myshop.services.impl.OrderServicesImpl;
import com.gym.myshop.util.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ming
 * 2021/11/7
 * description:订单处理
 */
@WebServlet("/order")
public class OrderController extends BaseServlet{
    private OrderServices os = new OrderServicesImpl();
    private CartServices cs = new CartServicesImpl();
    private AddressSerives as = new AddressSerivesImpl();

    /**
     *@author:ming
     *@Date: 2021/11/8
     *@description下单时添加收货地址
     */
    public String addOrderAddress(HttpServletRequest request, HttpServletResponse response) {
        //用户验证登录
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        if (user == null) {
            return Const.REDIRECT + "login.jsp";
        }
        //1 获取用户购物车的数据
        List<Cart> carList = cs.getCarList(user.getUid());
        //2 获取用户的收货地址的数据
        List<Address> addList = as.getUserAddreeByUid(user.getUid());
        //3 保存request
        request.setAttribute("cartList", carList);
        request.setAttribute("addList", addList);
        //4 转发 订单信息中
        return Const.FORWARD + "order.jsp";
    }

    /**
     *@author:ming
     *@Date: 2021/11/8
     *@description:生成订单
     * aid地址实体的唯一主键列  uid用户实体的主键属性
     */
    public String addOrder(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        if (user == null) {
            return Const.REDIRECT + "login.jsp";
        }
        Integer aid = StrUtils.toInt(request.getParameter("aid"));
        //业务层进行订单生成
        Orders order = os.createOrder(aid, user.getUid());
        request.setAttribute("order", order);
        return Const.FORWARD + "orderSuccess.jsp";
    }
}
